//
//  LXAlertTimer.m
//  bgms_p
//
//  Created by lachesis-zhu on 16/5/12.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXAlertTimer.h"

@implementation LXAlertTimer

+ (void)registerLocalNotification:(NSDate *)alertTime notificationDic:(NSDictionary *)dic withBody:(NSString *)bodystring{
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置触发通知的时间
//    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:6];
//    NSLog(@"fireDate=%@",fireDate);
    
    notification.fireDate = alertTime;
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复的间隔
    notification.repeatInterval = NSCalendarUnitMinute * 10;
    
    // 通知内容
    notification.alertBody =  bodystring;
    notification.alertLaunchImage = [NSString stringWithFormat:@""];
    notification.applicationIconBadgeNumber = 1;
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知参数
    notification.userInfo = dic;
    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

+ (void)cancelLocalNotificationWithKey:(NSString *)value {
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[@"seqId"];
            // 如果找到需要取消的通知，则取消
            if ([info isEqualToString:value]) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;
            }
        }
    }
}

@end
